package com.mm.update.controller;

import com.mm.update.service.VosDeployService;
import com.mm.update.util.IpUtil;
import com.mm.update.util.LogUtil;
import com.mm.update.vo.DeployLog;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ClassName: WebController
 * Description:
 * date: 2025/2/19 9:26
 *
 * @author  
 * @since JDK 1.8
 */

@Controller
@Data
@Slf4j
public class WebController {

    @Resource
    private VosDeployService vosDeployService;


    @Resource
    HttpServletRequest request;

    @RequestMapping(value = "/uploadAndDeploy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    ResponseEntity<String> uploadAndDeploy(@RequestParam String ip, @RequestParam String username, @RequestParam String password, @RequestParam MultipartFile file) {
        try {
            log.error("发布记录,userip{},ip{},username{},password{},filename{}", IpUtil.getIpAddr(request),ip,username,password,file.getOriginalFilename());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LogUtil.printLog(ip+"|"+username+"|"+file.getOriginalFilename()+"|"+IpUtil.getIpAddr(request)+"|"+ sdf.format(new Date()));
//            Thread.sleep(10000);
            vosDeployService.uploadAndDeployJar(ip, username, password, file,new DeployLog(ip,IpUtil.getIpAddr(request), sdf.format(new Date()),file.getOriginalFilename(),username));
            return ResponseEntity.ok("上传并发布成功");
        } catch (Exception e) {
            log.error("发送失败,",e);
            return ResponseEntity.ok("上传失败" + e.getMessage());
        }
    }

    @GetMapping(value = "/index")
    String getDeployList(Model model) {
        List<DeployLog> deployList = vosDeployService.getDeployList1();// 调用原有接口方法
        model.addAttribute("deployList", deployList);
        return "index"; // 返回 Thymeleaf 模板名称
    }
}
